-- UIImproveNotify
-- Created by chengb Apr/20/2015
-- 能力提升提示界面

-- UIImproveNotify继承自Layer
UIImproveNotify = class("UIImproveNotify", function()
    return cc.Layer:create();
end);

function UIImproveNotify.create(title, content)
    return UIImproveNotify.new(title, content);
end

local resize;

-- 构造函数
function UIImproveNotify:ctor(title, content)
    self:setName("UIImproveNotify");

    local node = cc.CSLoader:createNode("layout/common/ImproveNotify.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 2);

    resize(node);

    -- 根据语言版本设置缩进
    local kerning1, kerning2 = 0, 0;

    if getLang() ~= "zh" and getLang() ~= "tw" then
        kerning1, kerning2 = -4, -4;
    end

    if getLang() == "fr" then
        kerning1, kerning2 = -6, -4;
    end

    local text = findChildByName(self.node, "CT/title");
    TextStyleM.setTextStyle(text, TextStyleM.TEXT_SIZE_LARGE3, TextStyleM.TEXT_COLOR_BRIGHT_YELLOW, true, kerning1, nil, nil, nil, nil, nil, TextStyleM.getTextScale(content, 15 * 2));
    text:setPositionX(-DESIGN_WIDTH);

    local contentLabel = findChildByName(self.node, "CT/content");
    TextStyleM.setTextStyle(contentLabel, TextStyleM.TEXT_SIZE_LARGE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true, kerning2, nil, nil, nil, nil, nil, TextStyleM.getTextScale(content, 15 * 2));
    contentLabel:setString(content);
    contentLabel:setPositionX(-DESIGN_WIDTH);
    contentLabel:setTextHorizontalAlignment(cc.TEXT_ALIGNMENT_CENTER);

    local function doEffect()
        self.isPlaying = true;
        self:playEffect(title, content);
    end

    performWithDelay(self, doEffect, 0.01);

    -- 注册任意地方点击返回事件
    local bgImage = findChildByName(self.node, "bg/bgImg");
    local function onBgBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            if self.isPlaying then
                self:showItRightNow();
                return;
            end

            UIMgr.getCurrentScene():removeFormByName("UIImproveNotify");
        end
    end
    AddBackEventHandler(self, bgImage, onBgBackClick);
end

-- 适配
resize = function(node)
    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");
end

-- 直接显示结果
function UIImproveNotify:showItRightNow()
    local bg = findChildByName(self.node, "bg/bgImg");
    bg:setOpacity(255);
    bg:stopAllActions();
    local delay = cc.DelayTime:create(2);
    local callFunc = cc.CallFunc:create(function()
        UIMgr.getCurrentScene():removeFormByName("UIImproveNotify");
    end);
    bg:runAction(cc.Sequence:create(delay, callFunc));

    self:stopAllActions();
    self.isPlaying = false;

    local text = findChildByName(self.node, "CT/title");
    text:setPosition(cc.p(0, text:getPositionY()));
    text:stopAllActions();

    local contentLabel = findChildByName(self.node, "CT/content");
    contentLabel:setPosition(cc.p(0, contentLabel:getPositionY()));
    contentLabel:stopAllActions();
end

-- 显示能力提升特效
function UIImproveNotify:playImproveEffect(title, content)
    local text = findChildByName(self.node, "CT/title");
    text:setString(title);

    local parentNode = text:getParent();
    local interval = 1;

    if getLang() ~= "zh" and getLang() ~= "tw" then
        interval = 5;
    end

    local titleFlyIn = cc.CallFunc:create(function()
        -- 标题飞入
        setTextRotate(string.getWCharCount(title), text, parentNode, 360, interval);
    end);

    local moveTo = cc.MoveTo:create(0.3, cc.p(0, text:getPositionY()));

    local delayTime = 0.9;
    if getLang() == "fr" then
        delayTime = 0.1;
    end

    local delay1 = cc.DelayTime:create(delayTime);
    local contentFlyIn = cc.CallFunc:create(function()
        playEffect(text, 1135, 20, 180);

        -- 内容显示
        if content ~= nil and content ~= "" then

            local contentLabel = findChildByName(self.node, "CT/content");
            contentLabel:setString(content);

            local delay1 = cc.DelayTime:create(0.5);
            local moveTo2 = cc.MoveTo:create(0.1, cc.p(0, contentLabel:getPositionY()));

            contentLabel:runAction(cc.Sequence:create(delay1, moveTo2));
        end
    end);

    text:runAction(cc.Sequence:create(titleFlyIn, moveTo, delay1, contentFlyIn));
end

-- 播放效果
function UIImproveNotify:playEffect(title, content)
    self:playImproveEffect(title, content);

    -- bg渐现
    local bg = findChildByName(self.node, "bg/bgImg");
    local fadeIn = cc.FadeIn:create(0.3);
    local delay = cc.DelayTime:create(3.5);
    local fadeOut = cc.FadeOut:create(0.3);
    local callFunc = cc.CallFunc:create(function()
        UIMgr.getCurrentScene():removeFormByName("UIImproveNotify");
    end);
    bg:runAction(cc.Sequence:create(fadeIn, delay, fadeOut, callFunc));
end

